package com.kefu.chat.vo;

import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.common.context.KefuContext;
import com.kefu.common.util.Assert;
import com.kefu.common.vo.AbstractSearchVo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel("用户会话查询")
public class ChatUserSessionSearchVo extends AbstractSearchVo{

	 @NotNull
	 @ApiModelProperty(name = "startTime", value = "会话时间开始")
	 private Long startTime;
	 @NotNull
	 @ApiModelProperty(name = "endTime", value = "会话时间结束")
	 private Long endTime;
	 
	 @ApiModelProperty(name = "userId", value = "客户ID")
	 private String userId;
	 
	 @ApiModelProperty(name = "customerId", value = "访客ID")
	 private String customerId;

	@Override
	public <T> QueryWrapper<T> builder() {
		Assert.isTrue(StringUtils.isEmpty(customerId) && StringUtils.isEmpty(userId), "客户ID和访客ID不能同时为空");
    	QueryWrapper<T> builder = Wrappers.query();
    	
        builder.ge("start_time", startTime);
        builder.le("start_time", endTime);
        builder.eq("tenant_id", KefuContext.getTid());
        if(!StringUtils.isEmpty(customerId) && !StringUtils.isEmpty(userId)) {
        	builder.and(sub->{
        		sub.eq("user_id", userId).or().eq("customer_id", customerId);
        	});
        }else if(StringUtils.isEmpty(customerId)) {
        	 builder.eq("user_id", userId);
        }else {
        	 builder.eq("customer_id", customerId);
        }
        
        builder.orderByDesc("start_time");
        return builder;

    
	}
	 
	 
}
